<template>
  <div class="editor-wrapper">
    <!-- 工具栏 -->
    <Toolbar
      id="toolbar-container"
      :editor="editorRef"
      :default-config="toolbarConfig"
      :mode="mode"
      :style="{
        display: toolbarShow ? 'block' : 'none',
        border: `1px solid #ccc`,
      }"
    />
    <!-- 编辑器 -->
    <Editor
      id="editor-container"
      v-model="modelValue"
      :default-config="editorConfig"
      :mode="mode"
      @on-change="handleChange"
      @on-created="handleCreated"
      :style="
        isBorder
          ? {
              border: `1px solid #ccc`,
              width: width,
              height: height,
            }
          : {}
      "
    />
  </div>
</template>

<script setup lang="ts">
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";

// API 引用
import { uploadFileApi } from "@/api/file";

const props = defineProps({
  modelValue: {
    type: [String],
    default: "",
  },
  // 宽度
  width: {
    type: String,
    default: "800px",
  },
  // 高度
  height: {
    type: String,
    default: "300px",
  },
  // 是否只读
  readonly: {
    type: Boolean,
    default: false,
  },
  // 工具栏是否显示
  toolbarShow: {
    type: Boolean,
    default: true,
  },
  //边框是否显示
  isBorder: {
    type: Boolean,
    default: true,
  },
});

const emit = defineEmits(["update:modelValue", "update:fileUrl"]);

const modelValue = useVModel(props, "modelValue", emit);

const editorRef = shallowRef(); // 编辑器实例，必须用 shallowRef
const mode = ref("default"); // 编辑器模式
const toolbarConfig = ref({}); // 工具条配置
const fileUrl: any = ref([]);
// 编辑器配置
const editorConfig = ref({
  placeholder: "请输入内容...",

  MENU_CONF: {
    uploadImage: {
      // 自定义图片上传
      async customUpload(file: any, insertFn: any) {
        uploadFileApi(file).then((response) => {
          const url = `${
            import.meta.env.VITE_APP_BASE_API
          }/system/fileCenter/preview/file/${response.data.fileName}`;

          insertFn(url);
        });
      },
    },
    uploadVideo: {
      // 自定义视频上传
      async customUpload(file: any, insertFn: any) {
        uploadFileApi(file).then((response) => {
          const url = `${
            import.meta.env.VITE_APP_BASE_API
          }/system/fileCenter/preview/file/${response.data.fileName}`;

          insertFn(url);
        });
      },
    },
  },
});

const handleCreated = (editor: any) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
  props.readonly == true ? editor.disable() : editor.enable();
};

function handleChange(editor: any) {
  // 校验是否为空
  // const gg = /^<p>\s*<\/p>$/;
  if (editor.getHtml() === "<p><br></p>") {
    emit("update:modelValue", "");
  } else {
    modelValue.value = editor.getHtml();

    // emit("update:modelValue", editor.getHtml());
    emit("update:fileUrl", fileUrl.value);
    props.readonly == true ? editor.disable() : editor.enable();
  }
  emit("update:fileUrl", fileUrl.value);
}

// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (editor == null) return;
  editor.destroy();
});
</script>

<style src="@wangeditor/editor/dist/css/style.css"></style>
